﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using web.bussines;
using web.data.entities;
using web.core.Models;
using web.core.ViewModels;
using web.component;

namespace web.core.Controllers
{
    public class pageLayoutClientController : baseController
    {
        #region main_menu 

        [ChildActionOnly]
        public ActionResult main_menu_create()
        {
            var menu_view = new ViewModels.MainMenuModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            //---- menu accodion
            StringBuilder strAcc = new StringBuilder();
            //product
            product_service proSvc = new product_service();
            List<v_category_product> list_parent = proSvc.GetListCategoryProductByParent(null, languageId);
            int pstt = 0;
            var lCount = list_parent.Count;
            foreach (var pitem in list_parent)
            {
                string strPcss = "";
                if (pstt == 0)
                    strPcss += " first";
                //if (pstt==(lCount-1))
                //    strPcss += " last";
                strAcc.Append(string.Format("<li class='level0 nav-5 level-top {0}'><a href='/{1}_{2}' class='level-top'><span>{3}</span> </a></li>",
                                                strPcss, pitem.category_alias, pitem.category_id, pitem.category_name));
            }
            //tin
            strAcc.Append(string.Format("<li class='level0 nav-5 level-top last'><a href='/tin_tuc.ub' class='level-top'><span>Tin tức</span> </a></li>"));
            //---- menu full
            StringBuilder strFull = new StringBuilder();
            pstt = 0;
            //lCount  = list_parent.Count;
            foreach (var pitem in list_parent)
            {
                string strPcss = "";
                if (pstt == 0)
                    strPcss += " first";
                else if (pstt == (lCount - 1))
                    strPcss += " last";
                strFull.Append(string.Format("<li class='level0 nav-5 level-top {0}'><a href='/{1}_{2}' class='level-top'><span>{3}</span> </a>",
                                                strPcss, pitem.category_alias, pitem.category_id, pitem.category_name));

                strFull.Append(BuilChildMenu(pitem.category_id, languageId, 0, "nav-2" + (pstt + 1)));
                strFull.Append("</li>");
                pstt++;
            }
            //custom block 
            var list_link_block = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.CUSTOM_BLOCK)).Take(4);
            int iblock = 0;
            StringBuilder strBlock = new StringBuilder();
            foreach (var bitem in list_link_block)
            {
                strBlock.Append("<div class=\"grid12-3\">");
                if (!string.IsNullOrEmpty(bitem.image_path))
                {
                    strBlock.Append(string.Format(" <img src=\"{0}\" alt=\"{1}\" />", Ufile.GetImage(bitem.image_path), bitem.title));
                    strBlock.Append(string.Format(" <h5 class=\"heading\">{0}</h5>", bitem.title));
                }
                else
                {
                    strBlock.Append(string.Format(" <h3 class=\"heading\">{0}</h3>", bitem.title));
                }

                strBlock.Append(string.Format(" <p>{0}</p>", bitem.description));
                string link_css = "";
                if (iblock == 0)
                    link_css = "style=\"color: red;\"";

                strBlock.Append(string.Format(" <a href='{0}' {1}>Chi tiết</a>", bitem.link, link_css));

                strBlock.Append("</div>");
                iblock++;
            }

            //return to view
            menu_view.strMenuAccordion = strAcc.ToString();
            menu_view.strMenuFull = strFull.ToString();
            menu_view.strCustomBlock = strBlock.ToString();

            //menu_view.strMenu = accountModels.MenuBuilderAdmin(null, int.Parse(Session["user_id"].ToString()), Request.Url.ToString());
            //menu_view.controller_name = ControllerName;
            return PartialView("../Shared/commont/main_menu", menu_view);
        }

        private string BuilChildMenu(int cate_parent, int? languageId, int level, string catnav)
        {
            var str = new StringBuilder();
            var proSvc = new product_service();
            var list_parent = proSvc.GetListCategoryProductByParent(cate_parent, languageId);
            var lcount  = list_parent.Count;
            var pstt = 0;
            if (lcount > 0) {
                str.Append(string.Format("<ul class='level{0}'>", level));
            }
            foreach (var pitem in list_parent)
            {
                string strPcss = "";
                if (pstt == 0)
                    strPcss += " first";
                else if (pstt == (lcount - 1))
                    strPcss += " last";
                if (level == 0)
                    strPcss += " parent item";
                string cat_nav = catnav + "-"+(pstt + 1);//"nav-2-" + (pstt + 1)

                str.Append(string.Format("<li class='level{0} {1} {2}'><a href='/{3}_{4}'><span>{5}</span> </a>",
                                    (level + 1), cat_nav, strPcss, pitem.category_alias, pitem.category_id, pitem.category_name));

                str.Append(BuilChildMenu(pitem.category_id, languageId, (level + 1), cat_nav));

                str.Append("</li>");
                pstt++;
            }
            if (lcount > 0)
            {
                str.Append(string.Format("</ul>"));
            }

            return str.ToString();
        }
        #endregion

        #region menu product
        [ChildActionOnly]
        public ActionResult menu_category_product()
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var menu_view = new ViewModels.MenuCategoryProductViewModels();
            menu_view.sb = getCategoryProductMenuBuilder(null, languageId, Request.Url.ToString());
            menu_view.controller_name = ControllerName;
            return PartialView("../Shared/product/menuCategoryProduct", menu_view);
        }
        //
        public StringBuilder getCategoryProductMenuBuilder(int? cateId, int? languageId, string uri)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            return getListCategoryProductMenuBuilder(ref sb, cateId, languageId, 0, uri);
        }
        //
        public StringBuilder getListCategoryProductMenuBuilder(ref StringBuilder sb, int? cateId, int? languageId, int level, string uri)
        {
            Uri nuri = new Uri(uri);
            var arr = nuri.PathAndQuery.Split('/');
            product_service proSvc = new product_service();
            var i = 0;
            var max_parent_index = 0;
            var max_child_index = 0;
            //get list Category parent
            List<v_category_product> list_parent = proSvc.GetListCategoryProductByParent(cateId, languageId);
            max_parent_index = list_parent.Count;
            if (level == 0)
            {
                sb.Append("<ul class=\"accordion vertnav clearer\">");
            }
            else
            {
                sb.Append("<ul class=\"level" + (level - 1) + "\" style=\"display: block;\">");
            }
            sb.Append("\n");
            foreach (var item in list_parent)
            {

                List<v_category_product> list_child = proSvc.GetListCategoryProductByParent(item.category_id, languageId);
                max_child_index = list_child.Count;
                if (item.category_alias != null)
                {
                    sb.Append("<li class=\"level" + (level == 0 ? level : (level - 1)) + " nav-" + (i + 1) + "  " + (uri.Contains(item.category_alias) ? "active" : "") + "  " + (level == 0 ? "level-top" : "") + " " + (i == max_parent_index ? "last" : (i == 0 ? "first" : "")) + " " + (max_child_index > 0 ? "parent" : "") + " \">");
                }
                else
                {
                    sb.Append("<li class=\"level" + (level == 0 ? level : (level - 1)) + " nav-" + (i + 1) + "  " + (level == 0 ? "level-top" : "") + " " + (i == max_parent_index ? "last" : (i == 0 ? "first" : "")) + " " + (max_child_index > 0 ? "parent" : "") + " \">");
                }
                sb.Append("<a href=\"/" + item.category_alias + "_" + item.category_id + "\" class=\"level-top\"><span>" + item.category_name + "</span></a>");
                if (max_child_index > 0)
                {
                    sb.Append("<span class=\"opener\">&nbsp;</span>");
                    getListCategoryProductMenuBuilder(ref sb, item.category_id, languageId, (level + 1), uri);
                }
                sb.Append("</li>");

                i++;
            }
            sb.Append("</ul>");

            return sb;
        }

        #endregion

        #region menu_category_article
        [ChildActionOnly]
        public ActionResult menu_category_article()
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var menu_view = new ViewModels.MenuCategoryArticleViewModels();
            menu_view.sb = getCategoryArticleMenuBuilder(null, languageId, Request.Url.ToString());
            menu_view.controller_name = ControllerName;
            return PartialView("../Shared/article/menuCategoryArticle", menu_view);
        } 

        private StringBuilder getCategoryArticleMenuBuilder(int? cateId, int? languageId, string uri)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            return getCategoryArticleMenuBuilder(ref sb, cateId, languageId, 0, uri);
        }
        private StringBuilder getCategoryArticleMenuBuilder(ref StringBuilder sb, int? cateId, int? languageId, int level, string uri)
        {
            Uri nuri = new Uri(uri);
            var arr = nuri.PathAndQuery.Split('/');
            article_service artSvc = new article_service();
            var i = 0;
            var max_parent_index = 0;
            var max_child_index = 0;
            //get list Category parent
            List<v_category_article> list_parent = artSvc.GetListCategoryArticleByParent(cateId, languageId);
            max_parent_index = list_parent.Count;
            if (level == 0)
            {
                sb.Append("<ul class=\"accordion vertnav clearer\">");
            }
            else
            {
                sb.Append("<ul class=\"level" + (level - 1) + "\" style=\"display: block;\">");
            }
            sb.Append("\n");
            foreach (var item in list_parent)
            {

                List<v_category_article> list_child = artSvc.GetListCategoryArticleByParent(item.category_id, languageId);
                max_child_index = list_child.Count;
                if (item.category_alias != null)
                {
                    sb.Append("<li class=\"level" + (level == 0 ? level : (level - 1)) + " nav-" + (i + 1) + "  " + (uri.Contains(item.category_alias) ? "active" : "") + "  " + (level == 0 ? "level-top" : "") + " " + (i == max_parent_index ? "last" : (i == 0 ? "first" : "")) + " " + (max_child_index > 0 ? "parent" : "") + " \">");
                }
                else
                {
                    sb.Append("<li class=\"level" + (level == 0 ? level : (level - 1)) + " nav-" + (i + 1) + "  " + (level == 0 ? "level-top" : "") + " " + (i == max_parent_index ? "last" : (i == 0 ? "first" : "")) + " " + (max_child_index > 0 ? "parent" : "") + " \">");
                }
                sb.Append("<a href=\"/" + item.category_alias + "_" + item.category_id + ".ub\" class=\"level-top\"><span>" + item.category_name + "</span></a>");
                if (max_child_index > 0)
                {
                    sb.Append("<span class=\"opener\">&nbsp;</span>");
                    getCategoryArticleMenuBuilder(ref sb, item.category_id, languageId, (level + 1), uri);
                }
                sb.Append("</li>");

                i++;
            }
            sb.Append("</ul>");

            return sb;
        }
        #endregion

        #region header area  - khu vuc head top cua page
        [ChildActionOnly]
        public ActionResult header_top()
        { 
            string lang_code = DefaultLanguageGetter();
            var uview = new ViewModels.HeaderTopModels()
            {
                strSupport = Uconfig.GetConfig(global_const.SUPPORT, lang_code)
            };

            return PartialView("../Shared/commont/header_top", uview);
        }
        #endregion

        #region home_slide_show
        [ChildActionOnly]
        public ActionResult home_slide_show()
        {
            var menu_view = new ViewModels.HomeSlideShowModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);

            //main banner
            var lstSlide = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.SLIDE_HOME));
            StringBuilder strSlide = new StringBuilder();
            foreach (var sitem in lstSlide)
            {
                strSlide.Append(string.Format("<li class='slide'><a href='{0}'>", sitem.link));

                strSlide.Append(string.Format(" <img src='{0}' alt='{1}' />", Ufile.GetImage(sitem.image_path), sitem.title));

                strSlide.Append("<div class='caption light1 bottom-right'>");
                strSlide.Append(string.Format(" <h2 class='heading permanent'>{0}</h2>", sitem.title));
                strSlide.Append(string.Format(" <p>{0}</p>", sitem.description));
                strSlide.Append("</div>");

                strSlide.Append(" </a></li>");

            }
            // banner
            var lstBanner = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.ADVS_HOME_TOP1));
            StringBuilder strBanner = new StringBuilder();
            foreach (var bitem in lstBanner)
            {
                strBanner.Append(string.Format("<a class='banner' href='{0}' title='{1}'>", bitem.link, bitem.title));
                strBanner.Append(string.Format(" <img src='{0}' alt='{1}' />", Ufile.GetImage(bitem.image_path), bitem.title));

               // strBanner.Append("<div class='caption light1 full-width right'>");
               // strBanner.Append(string.Format(" <p class='right'>{0}</p>", bitem.title));
               // strBanner.Append("</div>");

                strBanner.Append(" </a>");
            }

            //return to view
            menu_view.strMainSlide = strSlide.ToString();
            menu_view.strSlideBanner = strBanner.ToString();

            //menu_view.strMenu = accountModels.MenuBuilderAdmin(null, int.Parse(Session["user_id"].ToString()), Request.Url.ToString());
            //menu_view.controller_name = ControllerName;
            return PartialView("../Shared/home/slideshow", menu_view);
        }
        #endregion

        #region home bannerAd1
        [ChildActionOnly]
        public ActionResult bannerAd1()
        {
            var menu_view = new ViewModels.BannerAdvsModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery); 

            // banner
            var lstBanner = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.ADVS_HOME_TOP2));
            StringBuilder strBanner = new StringBuilder();
            foreach (var bitem in lstBanner)
            {
                strBanner.Append(GetBanner(bitem, "grid12-4 banner"));
            }

            //return to view
            menu_view.strBanner = strBanner.ToString();

            return PartialView("../Shared/home/bannerAd1", menu_view);
        }

        private string GetBanner( web_link bitem, string bclass) {
            StringBuilder strBanner = new StringBuilder();
            strBanner.Append("<div class='grid12-4 banner'>");
            strBanner.Append(string.Format("<a href='{0}' title='{1}'>", bitem.link, bitem.title));
            strBanner.Append(string.Format(" <img src='{0}' alt='{1}' />", Ufile.GetImage(bitem.image_path), bitem.title));
            strBanner.Append(" </a></div>");
            return strBanner.ToString();
        }
        #endregion

        #region home bannerAd2
        [ChildActionOnly]
        public ActionResult bannerAd2()
        {
            var menu_view = new ViewModels.BannerAdvsModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);

            // banner
            var lstBanner = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.ADVS_BOTTOM));
            StringBuilder strBanner = new StringBuilder();
            foreach (var bitem in lstBanner)
            {
                strBanner.Append(GetBanner(bitem, "grid12-3 banner hide-below-768"));
            }

            //return to view
            menu_view.strBanner = strBanner.ToString();

            return PartialView("../Shared/home/bannerAd2", menu_view);
        }
        #endregion

        #region common bannerAd3
        [ChildActionOnly]
        public ActionResult bannerAd3()
        {
            var menu_view = new ViewModels.BannerAdvsModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);

            // banner
            var lstBanner = weblinkModels.list_by_type((int)languageId, Ustring.GetEnumIdToString(global_const.E_WEB_LINK.ADVS_BOTTOM_PAGE));
            StringBuilder strBanner = new StringBuilder();
            foreach (var bitem in lstBanner)
            {
                strBanner.Append(GetBanner(bitem, "grid12-6 banner"));
            }

            //return to view
            menu_view.strBanner = strBanner.ToString();

            return PartialView("../Shared/commont/bannerAd3", menu_view);
        }
        #endregion

        #region product_testimonials_list
        [ChildActionOnly]
        public ActionResult product_testimonials_list()
        {
            var menu_view = new ViewModels.LeftListTestimonialsModels();
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);

            // banner
            var lstIdea = clientTestimonialsModels.client_testimonial_list_by_status(1, (int)languageId); 

            StringBuilder strIdea = new StringBuilder();
            foreach (var item in lstIdea)
            {
                strIdea.Append("<div class='block-content sample-block'>");
                strIdea.Append(string.Format("<p>\"<i>{0}</i>\"</p>", item.client_comment));
                strIdea.Append(string.Format("<p style='text-align:right;'>{0}</p>", item.client_name));
                strIdea.Append("</div>");
            }

            //return to view
            menu_view.strListTestimonials = strIdea.ToString();

            return PartialView("../Shared/product/product_testimonials", menu_view);
        }
        #endregion

        #region footer area
        [ChildActionOnly]
        public ActionResult footer_bottom()
        {
            string lang_code = DefaultLanguageGetter();
            var uview = new ViewModels.FooterBottomModels()
            {
                //primary footer
                footer_primary1 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter1", lang_code),
                footer_primary2 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter2", lang_code),
                footer_primary3 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter3", lang_code),
                footer_primary4 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter4", lang_code),
                social_links = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "social_links", lang_code),
                //second footer
                footer_sec1 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter_sec1", lang_code),
                footer_sec2 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter_sec2", lang_code),
                footer_sec3 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter_sec3", lang_code),
                footer_sec4 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter_sec4", lang_code),
                //copyright
                page_footer = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, "pagefooter", lang_code)

            };

            return PartialView("../Shared/commont/footer_bottom", uview);
        }
        #endregion

        #region list doi tac - home
        [ChildActionOnly]
        public ActionResult home_partner_list()
        {
            string lang_code = DefaultLanguageGetter();
            int languageId = (int)languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var uview = new ViewModels.HomePartnerModels()
            {
                list_partner = partnerModels.partner_list(languageId)
            };

            return PartialView("../Shared/home/partner", uview);
        }
        #endregion

        #region list product tag
        [ChildActionOnly]
        public ActionResult list_product_tag()
        {
            var list_tag = productModels.GetListProductTag();

            return PartialView("../Shared/commont/list_product_tag", list_tag);
        }
        #endregion

        #region shopping guide
        [ChildActionOnly]
        public ActionResult shopping_guide()
        {
            string lang_code = DefaultLanguageGetter();
            var sview = new ViewModels.ShoppingGuideModels()
            {
                guide1 = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PRODUCT, "guid1", lang_code),
                social_share = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PRODUCT, "social_share", lang_code),
                logo_shopping_guide = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PRODUCT, "logo_shopping_guide", lang_code)
            };

            return PartialView("../Shared/commont/shopping_guide", sview);
        }
        #endregion

        
    }
}
